package com.liu.my_blog_system.config;

import com.google.gson.Gson;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Random;

/**
 * @author liuchengjie
 */
@Configuration
public class MyWebMvcConfig implements WebMvcConfigurer {

    /**
     * 注入拦截器
     * @return
     */
    @Bean
    public MyInterceptor myInterceptor() {
        return new MyInterceptor();
    }

    /**
     * 注入加密工具类
     * @return
     */
    @Bean
    public BCryptPasswordEncoder cryptPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

    /**
     * 注入随机数类
     * @return
     */
    @Bean
    public Random random(){
        return new Random();
    }

    /**
     * 注入gson
     * @return
     */
    @Bean
    public Gson gson() {
        return new Gson();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myInterceptor())
                .addPathPatterns("/admin/**");
    }
}
